import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../views/Login";
import Home from "../views/Home";
import User from "../views/User";
import Regist from "../views/Regist";

Vue.use(VueRouter)

const routes = [
  {
    path : '/',
    name : 'Login',
    component : Login
  },
  {
    path : '/home',
    name : 'Home',
    component : Home
  },
  {
    path : '/regist',
    name : 'Regist',
    component: Regist
  },
  {
    path : '/login',
    name : 'Login',
    component : Login
  },
  {
    path : '/user',
    name : 'User',
    component : User
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

// 监督是否有该token
// 这里就暂时十分简陋吧，就简单搞个登录判断，就离线存储设置个值
// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  console.log("path " ,to.path)

  if (to.path === '/login') next();
  else if(to.path === '/regist') next();
  else {
    let token = localStorage.getItem('borrowToken');
    if (token == "false") {
      next('/login');
    } else {
      next();
    }
  }
});

export default router
